package com.qcby.demo.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.demo.base.dto.DemoDTO;
import com.qcby.demo.base.mapper.DemoMapper;
import com.qcby.demo.base.po.DemoPO;
import com.qcby.demo.base.service.ITestService;
import com.qcby.framework.common.enums.DeleteFlagEnum;
import com.qcby.framework.common.pojo.PageParam;
import com.qcby.framework.common.pojo.PageResult;
import com.qcby.framework.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试服务impl
 *
 * @author cong.zhen
 * @date 2023/04/11
 */
@Service
@Slf4j
public class TestServiceImpl implements ITestService {

    @Resource
    private DemoMapper demoMapper;


    @Override
    public Boolean handInPapers(DemoDTO examDTO) {
        //todo 此处应该写交卷的逻辑
        log.info("交卷");
        return Boolean.TRUE;
    }

    @Override
    public Result<PageResult<DemoDTO>> demoPageQuery(DemoDTO demoDTO, PageParam pageParam){

        /**构造查询条件**/
        LambdaQueryWrapper<DemoPO> lqw = buildWrapper(demoDTO);
        Page<DemoPO> demoPOPage = demoMapper.selectPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lqw);
        /**po转dto**/
        List<DemoDTO> demoDTOS = BeanUtil.copyToList(demoPOPage.getRecords(),DemoDTO.class);
        /**构造返回体**/
        PageResult pageResult = new PageResult();
        pageResult.setList(demoDTOS);
        pageResult.setTotal(demoPOPage.getTotal());
        return Result.getSuccessResult(pageResult);
    }

    @Override
    public DemoDTO getDemoById(Long id) {
        LambdaQueryWrapper<DemoPO> lqw = new LambdaQueryWrapper<DemoPO>().eq(DemoPO::getId,id).eq(DemoPO::getDelFlag,DeleteFlagEnum.NO_DELETE.getCode());
        DemoPO demoPO = demoMapper.selectOne(lqw);
        return BeanUtil.copyProperties(demoPO,DemoDTO.class);
    }

    @Override
    public Boolean saveOrUpdate(DemoDTO demoDTO) {
        DemoPO demoPO = BeanUtil.copyProperties(demoDTO,DemoPO.class);
        int temp;
        /**如果有id就是更新，没有id就是保存**/
        if(ObjectUtil.isNull(demoDTO.getId())){
            temp = demoMapper.insert(demoPO);
        }else {
            temp = demoMapper.updateById(demoPO);
        }
       return temp > 0;
    }

    @Override
    public Boolean removeById(Long id) {
        LambdaUpdateWrapper<DemoPO> updateWrapper = new LambdaUpdateWrapper<DemoPO>().eq(DemoPO::getId,id).set(DemoPO::getDelFlag, DeleteFlagEnum.DELETE.getCode());
        int update = demoMapper.update(null, updateWrapper);
        return update > 0;
    }

    public LambdaQueryWrapper<DemoPO> buildWrapper(DemoDTO demoDTO){
        LambdaQueryWrapper<DemoPO> lqw = new LambdaQueryWrapper<DemoPO>();
        /**此处写自己的分页查询参数**/
        lqw.eq(StrUtil.isNotBlank(demoDTO.getExamNo()),DemoPO::getExamNo, demoDTO.getExamNo());
        lqw.eq(DemoPO::getDelFlag,DeleteFlagEnum.NO_DELETE.getCode());
        /**根据id去排序，不要根据创建时间**/
        lqw.orderByDesc(DemoPO::getId);
        return lqw;
    }

}
